import { getMongoRepository, } from "typeorm";

import { Select_histories } from "../models/SelectHistories";

class SelectHistoriesService {

  // 保存退课和选课的操作（在选课退退的操作中调用）
  save(histories: any) {
    const selectHistoriesRepository = getMongoRepository(Select_histories);
    return selectHistoriesRepository.save(histories)
  }

  // 查询所有历史
  find(stu: string) {
    const selectHistoriesRepository = getMongoRepository(Select_histories);
    return selectHistoriesRepository.find({
      where: {
        stu_num: stu
      },
      order: { selectAt: 'DESC' }
    })
  }

  // 清除历史
  deleteById(id: string) {
    const selectHistoriesRepository = getMongoRepository(Select_histories);
    return selectHistoriesRepository.delete({ id })
  }

  // 删除课程后，也要同时删除选课记录中的对应课程
  deleteManyByCid(cid: string) {
    const selectHistoriesRepository = getMongoRepository(Select_histories);
    return selectHistoriesRepository.deleteMany({ cid })
  }

  // 根据课程id进行查询
  findByCid(cid: string) {
    const selectHistoriesRepository = getMongoRepository(Select_histories)
    return selectHistoriesRepository.findOne({ cid })
  }
}

export default new SelectHistoriesService()